
//
//  ByForgetPwdVC.m
//  Homept_ios
//
//  Created by mc on 16/9/2.
//  Copyright © 2016年 pro. All rights reserved.
//

#import "ByForgetPwdVC.h"
// vc
#import "ByFillCodeVC.h"            // 验证验证码
// view
#import "ByTextField.h"             // tf
// vm
#import "ByLoginViewModel.h"        // vm
@interface ByForgetPwdVC ()<FetchCodeDelegate>
{
    ByLoginViewModel *_viewmodel;         // vm
}
@end

@implementation ByForgetPwdVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"忘记密码";
    [self addLeftBackBarButtonItem];
    [self createView];
    
    _viewmodel = [[ByLoginViewModel alloc] init];
    _viewmodel.fetchcodeDelegate = self;
}
#pragma mark ----创建视图
- (void)createView {
    CGFloat x,y,width,height;
    x = 30;
    y = 20;
    width = SCREEN_WIDTH - 2*x;
    height = LineHeight;
    ByTextField *field = [[ByTextField alloc] initWithFrame:CGRectMake(x, y, width, height)];
    UIImageView *leftImg = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, LineHeight - 10, LineHeight - 10)];
    [leftImg setImage:[UIImage imageNamed:@"手机"]];
    UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, LineHeight, LineHeight)];
    [leftView addSubview:leftImg];
    field.tag = 100;
    field.leftView = leftView;
    field.leftViewMode = UITextFieldViewModeAlways;
    field.keyboardType = UIKeyboardTypeNumberPad;
    NSMutableAttributedString *paleceholderAttr = [[NSMutableAttributedString alloc] initWithString:@"请输入手机号"];
    [paleceholderAttr addAttribute:NSFontAttributeName value:FONT(13) range:NSMakeRange(0, paleceholderAttr.length)];
    [field setAttributedPlaceholder:paleceholderAttr];
    [self.view addSubview:field];
    
    
    y = field.bottom + 5;
    height = 1;
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)];
    line.backgroundColor = [UIColor colorWithWhite:0.90 alpha:1];
    [self.view addSubview:line];
    
    y = line.bottom + 20;
    UIButton *nextBtn = [[UIButton alloc] initWithFrame:CGRectMake(x, y, width, 35)];
    [nextBtn setTitle:@"下一步" forState:UIControlStateNormal];
    nextBtn.backgroundColor = HTB_THEME_COLOR;
    [nextBtn addTarget:self action:@selector(nextAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:nextBtn];
    
}
#pragma mark ---点击事件
- (void)nextAction:(UIButton *)btn {
    ByTextField *field = [self.view viewWithTag:100];
    
    if ([field.text isStringNull]) {
        [ByCoreToolAlert showOnlyText:@"请输入手机号"];
        return;
    }
    if (field.text.length != 11) {
        [ByCoreToolAlert showOnlyText:@"请输入正确手机号"];
        return;
    }
    ByNetParamsModel *model = [[ByNetParamsModel alloc] init];
    model.mobile = field.text;
    model.type = @"2";
    [_viewmodel fetchCode:model];

}

#pragma mark ---相关代理
- (void)fetchCodeSuccess:(ByNetBaseResult *)result{
    [ByCoreToolAlert showOnlyText:result.data];
    ByTextField *field = [self.view viewWithTag:100];
    ByFillCodeVC *vc = [[ByFillCodeVC alloc] init];
    vc.phoneNumber = field.text;
    vc.type = @"2";
    [self by_normalPush:vc hasNavi:YES];
}
- (void)fetchCodeFail:(ByNetBaseResult *)result{
    [ByCoreToolAlert showFailureWithText:result.msg];
}


@end
